package com.express.e.root.service.impl;

import com.express.e.root.bean.Admin;
import com.express.e.root.dao.impl.AdminDaoImpl;
import com.express.e.root.service.AdminService;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminDaoImpl adminDao = new AdminDaoImpl();

    @Override
    public Admin addAdmin(Admin admin) {
        Boolean addFlag = adminDao.addAdmin(admin);
        if (addFlag) {
            return adminDao.queryByName(admin.getAdminName());
        }
        return null;
    }

    @Override
    public Boolean deleteAdmin(Integer adminId) {
        return adminDao.deleteAdmin(adminId);
    }

    @Override
    public Admin updateAdmin(Admin admin) {
        Boolean updateFlag = adminDao.updateAdmin(admin);
        if (updateFlag) {
            return adminDao.queryByName(admin.getAdminName());
        }
        return null;
    }

    @Override
    public List adminList() {
        return adminDao.listAdmin();
    }

    @Override
    public Admin queryById(Integer adminId) {
        return adminDao.queryById(adminId);
    }

    @Override
    public Admin queryByPhone(String phone) {
        return adminDao.queryByPhone(phone);
    }

    @Override
    public Admin queryByName(String adminName) {
        return adminDao.queryByName(adminName);
    }

    @Override
    public Admin queryByCardId(String cardId) {
        return adminDao.queryByCardId(cardId);
    }

    @Override
    public Admin loginIn(Admin admin) {
        Admin admin4LoginIn = adminDao.queryByAdmin(admin);
        if (admin4LoginIn.getAdminId() != null) {
            if (adminDao.onlineAdmin(admin)) {
                return admin4LoginIn;
            }
        }
        return null;
    }

    @Override
    public Boolean loginOut(Admin admin) {
        Boolean flag4LoginOut = adminDao.loginOutByAdmin(admin);
        if (flag4LoginOut) {
            if (adminDao.queryByAdmin(admin).getState() == 0) {
                return true;
            }
        }
        return false;
    }
}
